<template>
  <div class="todo-footer">
    <label>
      <input type="checkbox"  v-model="checkAllBtn" />
    </label>
    <span>
      <span>已完成{{ completedTodos }}</span> / 全部{{ checkAll }}</span
    >
    <button class="btn btn-danger" @click="clearAllCompleted">清除已完成任务</button>
  </div>
</template>

<script lang="ts">
import { computed, defineComponent } from "vue";
import { Todo } from "../types/Todo";

export default defineComponent({
  name: "Footer",
  props: {
    todos: {
      type: Array as () => Todo[],
      required: true,
    },
    checkAll: {
      type: Number ,
      required: true,
    },
    checkAllTodos: {
      type: Function ,
      required: true,
    },
    completedTodos: {
      type: Number,
      required: true,
    },
    deleteAllCompletedTodo: {
        type: Function ,
        required: true
    }
  },
  setup(props) {
    // console.log("todos,length = ", props.todos);
    
    // 清空所有已完成任务
    const clearAllCompleted = () => {
        if(window.confirm("您确定要清空所有已完成任务吗?")) {
            props.deleteAllCompletedTodo()
        }
    } 

    // 计算属性， 全选 or 全不选
    const checkAllBtn = computed({
        get() {
            return props.todos.length > 0 && props.todos.length === props.completedTodos
        },
        set(val) {
            props.checkAllTodos(val)
        }
    })

    return {
        clearAllCompleted,
        checkAllBtn,
    };
  },
});
</script>

<style scoped>
/*footer*/
.todo-footer {
  height: 40px;
  line-height: 40px;
  padding-left: 6px;
  margin-top: 5px;
}

.todo-footer label {
  display: inline-block;
  margin-right: 20px;
  cursor: pointer;
}

.todo-footer label input {
  position: relative;
  top: -1px;
  vertical-align: middle;
  margin-right: 5px;
}

.todo-footer button {
  float: right;
  margin-top: 5px;
}
</style>
